{% extends 'layout/base.html' %}

{% block content %}
<div class="row mt-5">
    <div class="col-lg-3">
        {% set active_nav = 'users' %}
        {% include 'config/includes/sidebar.html' %}
    </div>
    <div class="col-lg-7">
        <h4>{{ 'Create' if user_id == 0 else 'Edit' }} User</h4>

        {% if base_domain|length == 0 %}
        <div class="alert alert-danger text-center">
            <a href="{{ url_for('config.dns') }}">Please click here to set the DNS Base Domain before creating new users.</a>
        </div>
        {% endif %}

        <form action="{{ url_for('config.user_edit_save', user_id=user_id) }}" method="post">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">

            <div class="form-group row">
                <div class="col">
                    <label for="username">Username</label>
                </div>
                <div class="col">
                    <input type="text" class="form-control" name="username" id="username" value="{{ user.username if user_id > 0 }}" required autofocus>
                </div>
            </div>

            <div class="form-group row">
                <div class="col">
                    <div class="row">
                        <div class="col">
                            <label for="password">Password</label>
                        </div>
                        <div class="col">
                            <input type="password" class="form-control" name="password" id="password" value="{{ '********' if user_id > 0 }}" required>
                        </div>
                    </div>
                    <div class="row text-right">
                        <div class="col text-right"><small class="font-italic">{{ password_complexity }}</small></div>
                    </div>
                </div>
            </div>

            <div class="form-group row">
                <div class="col">
                    <label for="full_name">Full Name</label>
                </div>
                <div class="col">
                    <input type="text" class="form-control" name="full_name" id="full_name" value="{{ user.full_name if user_id > 0 }}" required>
                </div>
            </div>

            <div class="form-group row">
                <div class="col">
                    <label for="email">E-mail</label>
                </div>
                <div class="col">
                    <input type="email" class="form-control" name="email" id="email" value="{{ user.email if user_id > 0 }}" required>
                </div>
            </div>

            <div class="form-group row">
                <div class="col">
                    <label for="auth">Authentication</label>
                </div>
                <div class="col text-right">
                    <select class="custom-select form-control" name="auth" id="auth">
                        {% for id, name in auth_types.items() %}
                        <option value="{{ id }}" {{ 'selected' if user.auth_type_id == id }}>{{ name }}</option>
                        {% endfor %}
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <div class="col">
                    <label for="admin">Administrator</label>
                </div>
                <div class="col text-right">
                    <div class="custom-control custom-switch">
                        <input name="admin" type="checkbox" class="custom-control-input" id="admin" value="1" {{ 'checked' if user_id > 0 and user.admin }}>
                        <label class="custom-control-label" for="admin"></label>
                    </div>
                </div>
            </div>

            <div class="form-group row">
                <div class="col">
                    <label for="active">Active</label>
                </div>
                <div class="col text-right">
                    <div class="custom-control custom-switch">
                        <input name="active" type="checkbox" class="custom-control-input" id="active" value="1" {{ 'checked' if (user_id == 0) or (user_id > 0 and user.active) }}>
                        <label class="custom-control-label" for="active"></label>
                    </div>
                </div>
            </div>

            {% if user_id > 0 and user.has_2fa() %}
            <div class="form-group row">
                <div class="col">
                    <label for="2fa">Two Factor Authentication</label>
                </div>
                <div class="col text-right">
                    <div class="custom-control custom-switch">
                        <input name="2fa" type="checkbox" class="custom-control-input" id="2fa" value="1" checked>
                        <label class="custom-control-label" for="2fa"></label>
                    </div>
                </div>
            </div>
            {% endif %}

            {% if user_id == 0 %}
            <div class="form-group row">
                <div class="col">
                    <label for="create_master_zone">Create Master Zone</label>
                </div>
                <div class="col text-right">
                    <div class="custom-control custom-switch">
                        <input name="create_master_zone" type="checkbox" class="custom-control-input" id="create_master_zone" value="1" checked>
                        <label class="custom-control-label" for="create_master_zone"></label>
                    </div>
                </div>
            </div>
            {% endif %}

            <div class="form-group">
                <button type="submit" class="btn btn-block btn-primary">save</button>
            </div>
        </form>
    </div>
</div>
{% endblock %}